﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.Web.Components;
using System.Text;

namespace CardLogistics.Web.MainPage.MoneyCollect.MoneyCollect
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.CardArrival.Storage_Detailed_CardData dal = new DAL.CardArrival.Storage_Detailed_CardData();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            {
                BindRpt(0);
                BindDDL(); //绑定下拉框
            }
        }

        /// <summary>
        /// 绑定下拉框
        /// </summary>
        public void BindDDL()
        {
            //卡片类型
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult("select id,cardtypename from basic_cardtype where  status =0 and isdelete=0");
            if (dt.Rows.Count > 0)
            {
                //添加“请选择”
                DataRow dr = dt.NewRow();
                dr["id"] = 0;
                dr["cardtypename"] = "全部";
                dt.Rows.InsertAt(dr, 0);
                ddlCardTypeId.DataSource = dt;
                ddlCardTypeId.DataTextField = "cardtypename";//卡片类型名称
                ddlCardTypeId.DataValueField = "id";//卡片类型ID
                ddlCardTypeId.DataBind();
                //设置默认选中项 
                ddlCardTypeId.SelectedIndex = 0;
            }

            //芯片类型
            DataTable dtType = CardLogistics.DAL.DbHelperSQL.GetQueryResult("select id,typename from basic_basictype where typeno=0 and status =0 and isdelete=0");
            if (dtType.Rows.Count > 0)
            {
                //添加“请选择”
                DataRow dr = dtType.NewRow();
                dr["id"] = 0;
                dr["typename"] = "全部";
                dtType.Rows.InsertAt(dr, 0);
                ddlBasicTypeId.DataSource = dtType;
                ddlBasicTypeId.DataTextField = "typename";//芯片类型名称
                ddlBasicTypeId.DataValueField = "id";//芯片类型ID
                ddlBasicTypeId.DataBind();
                //设置默认选中项 
                ddlBasicTypeId.SelectedIndex = 0;
            }
        }


        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = hdfDelete.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                int resCount = 0;
                for (int i = 0; i < ids.Length; i++)
                {
                    string resStr = dal.Delete(ids[i]);
                    if (resStr.Split(',')[0] == "ok")
                        resCount++;
                }
                BindRpt(0);
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(int pageIndex)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" 1=1 ");
            if (txt_PConNo.Text.Trim().Length > 0)
                sbStr.Append(" and PConNo like '%" + txt_PConNo.Text.Trim() + "%' ");//合同编号
            if (txt_BatchNo.Text.Trim().Length > 0)
                sbStr.Append(" and BatchNo like '%" + txt_BatchNo.Text.Trim() + "%' ");//批次号
            if (txt_SuppliersName.Text.Trim().Length > 0)
                sbStr.Append(" and SuppliersName like '%" + txt_SuppliersName.Text.Trim() + "%' ");//供应商名称

            if (ddlCardTypeId.SelectedValue != "" && ddlCardTypeId.SelectedValue != "0")//卡片类型
                sbStr.Append(" and CardTypeId ='" + ddlCardTypeId.SelectedValue + "'");
            if (ddlBasicTypeId.SelectedValue != "" && ddlBasicTypeId.SelectedValue != "0")//芯片类型
                sbStr.Append(" and BasicTypeId ='" + ddlBasicTypeId.SelectedValue + "'");
            DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_moneycollect_card", "ID", " LastUpdateDate ", AspNetPager1.PageSize, pageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            if (dt.Rows.Count>0)
            {
                int TotalBoxnumber = 0; //箱数
                int TotalCardnumber = 0; //每箱盒数
                int TotalDueNumber = 0; //应有数量
                int TotalActualNumber = 0;//实际数量
                decimal TotalPrice = 0;//总价格

                if (dt.Rows[0]["Boxnumber"] != DBNull.Value)//箱数
                {
                    TotalBoxnumber = Convert.ToInt32(dt.Compute("sum(Boxnumber)", ""));
                }
                else
                    TotalBoxnumber = 0;

                if (dt.Rows[0]["Cardnumber"] != DBNull.Value)//每箱盒数
                {
                    TotalCardnumber = Convert.ToInt32(dt.Compute("sum(Cardnumber)", ""));
                }
                else
                    TotalCardnumber = 0;

                if (dt.Rows[0]["DueNumber"] != DBNull.Value)//应有数量
                {
                    TotalDueNumber = Convert.ToInt32(dt.Compute("sum(DueNumber)", ""));
                }
                else
                    TotalDueNumber = 0;

                if (dt.Rows[0]["ActualNumber"] != DBNull.Value)//实际数量
                {
                    TotalActualNumber = Convert.ToInt32(dt.Compute("sum(ActualNumber)", ""));
                }
                else
                    TotalDueNumber = 0;
                if (dt.Rows[0]["TotalPrice"] != DBNull.Value)//总价格
                {
                     TotalPrice = Convert.ToDecimal(dt.Compute("sum(TotalPrice)", ""));//总价格
                }
                else
                    TotalPrice = 0;

                DataRow dr = dt.NewRow();
                dr["PConNo"] = "合计";
                dr["Boxnumber"] = TotalBoxnumber;
                dr["Cardnumber"] = TotalCardnumber;
                dr["DueNumber"] = TotalDueNumber;
                dr["ActualNumber"] = TotalActualNumber;
                dr["TotalPrice"] = TotalPrice;
                dt.Rows.Add(dr);
            }

            if (dt.Rows.Count == 0 && pageIndex == 1)
            {
                ShowMessageBox("没有查询到符合条件的记录！");
            }
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt(AspNetPager1.CurrentPageIndex);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            BindRpt(1);
        }
    }
}